//
//  AppDelegate.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialAlipayShareHandler.h"
#import "LoginViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <EaseMob.h>

@interface AppDelegate ()<EMChatManagerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]init];
    RootViewController *rootVC = [[RootViewController alloc]init];
//    UINavigationController *rootNC = [[UINavigationController alloc]initWithRootViewController:rootVC];
    self.window.rootViewController = rootVC;
    self.window.backgroundColor = [UIColor greenColor];
    [self.window makeKeyAndVisible];
    
    //设置友盟分享
    [UMSocialData setAppKey:@"52b13dc556240b55870e7148"];
    //设置手机QQ的使用
    //    [UMSocialConfig setSnsPlatformNames:@[UMShareToSina,UMShareToTencent,UMShareToQzone,UMShareToQQ,UMShareToEmail]];
    //    [UMSocialQQHandler setQQWithAppId:@"1105134620" appKey:@"WanVnjTeMnlFKqw0" url:@"http://www.umeng.com/social"];
    
    //是由于您的QQ没有通过审核或者上线，随机报出的错误，这个你可以把QQ的信息填写完整，到开放平台添加个测试账号就可以了。
    //设置QQ的appId
    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
    
    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
    
    // 设置支付宝分享的appId
    [UMSocialAlipayShareHandler setAlipayShareAppId:@"2015111700822536"];
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    LoginViewController *presentVC = [storyBoard instantiateInitialViewController];
//    [self.window.rootViewController presentViewController:presentVC animated:NO completion:^{
//    
//    }];
    //集成leancloud存储服务
    [AVOSCloud setApplicationId:@"xMdyimbQrlecBmdlH3UXk65B-gzGzoHsz" clientKey:@"Bz5Nk4ww9ULMgThCg00CfevM"];

    //集成环信
    
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"yanfa9#demo" apnsCertName:nil];
    
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    //检测自动登录
    
    if([[EaseMob sharedInstance].chatManager isAutoLoginEnabled]){
        
        self.window.rootViewController = rootVC;
        
        
    }else{
        
        self.window.rootViewController = presentVC;
        
    }
    
    //添加代理
    
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];

    
    
    
    
    
    return YES;
}

//实现自动登录的回调
-(void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error{
    
    if (!error) {
        
        NSLog(@"自动登录成功");
        
        [AVUser logInWithUsername:loginInfo[@"username"] password:loginInfo[@"password"] error:nil];
        
    }else{
        
        NSLog(@"自动登录失败");
    }
    
}

//从其他设备登录
-(void)didLoginFromOtherDevice{
    
    
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
